package cn.lingyangwl.agile.infra.module.dict;

import cn.lingyangwl.agile.infra.module.dict.model.entity.DictItem;
import cn.lingyangwl.framework.data.mate.fieldbind.inter.IDataBind;
import cn.lingyangwl.framework.data.mate.fieldbind.model.DictType;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 数据库字段字典绑定
 * @author shenguangyang
 */
@Component
public class DictFieldBind implements IDataBind<Map<String, Map<String, DictItem>>> {
    private static final Map<String, DictItem> EMPTY_MAP = new HashMap<>();
    static DictItem EMPTY = new DictItem();
    @Resource
    private DictItemApi dictItemApi;

    @Override
    public Map<String, Map<String, DictItem>> getCodesOfTypes(List<DictType> dictTypes) {
        return dictItemApi.listByTypes(dictTypes.stream().map(DictType::getType).collect(Collectors.toList()));
    }

    @Override
    public String getCodeValue(Map<String, Map<String, DictItem>> codesOfTypes, String type, String code) {
        return codesOfTypes.getOrDefault(type, Collections.emptyMap())
            .getOrDefault(code, EMPTY).getItemLabel();
    }
}
